/*
 * @Author: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @Date: 2024-02-19 14:20:10
 * @LastEditors: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @LastEditTime: 2024-03-08 13:16:32
 * @FilePath: \hrManage\src\store\modules\user.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getUserInfo } from '@/api/user'
import { constantRoutes } from '@/router'
import { resetRouter } from '@/router'
const state = {
  token: getToken(),
  userInfo: {},
  routes: constantRoutes
}
const actions = {
  async login(context, value) {
    const token = await login(value)
    console.log(token)
    context.commit('setToken', token)
  },
  async getUserInfo(context) {
    const userInfo = await getUserInfo()
    context.commit('getUserInfo', userInfo)
    return userInfo
  },
  logOut(context) {
    context.commit('removeToken')
    resetRouter()
  }
}
const mutations = {
  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  removeToken(state, token) {
    state.token = ''
    state.userInfo = {}
    removeToken()
  },
  getUserInfo(state, value) {
    state.userInfo = value
  },
  updateRoutes(state, value) {
    state.routes = [...constantRoutes, ...value]
  }
}
const getters = {}
export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
}
